package com.yanqu.road.server.manger.mammon;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.decoration.UserMammonOfflineAdd;
import com.yanqu.road.entity.decoration.MammonRecord;
import com.yanqu.road.entity.enums.eChannelType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eRedHotNotifyType;
import com.yanqu.road.entity.enums.eServerGroup2Type;
import com.yanqu.road.entity.player.UserDecoration;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.servercenter.ServerGroup;
import com.yanqu.road.entity.servercenter.ServerGroup2;
import com.yanqu.road.logic.bussiness.player.UserDecorationBussiness;
import com.yanqu.road.logic.bussiness.player.UserMammonBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.DecorationPb;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cross.group.CrossGroupProto;
import com.yanqu.road.pb.decoration.DecorationProto;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.DecorationModule;
import com.yanqu.road.server.gameplayer.module.player.MammonModule;
import com.yanqu.road.server.manger.DataAnalyticsMgr;
import com.yanqu.road.server.manger.MammonConfigMgr;
import com.yanqu.road.server.manger.ServerGroup2Mgr;
import com.yanqu.road.server.manger.ServerGroupMgr;
import com.yanqu.road.server.manger.activity.ActivityInfoMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.cross.CrossDataMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.pb.WorldMessagePb;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.UUIDHelper;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class MammonMgr extends TempMgr {

    private static Map<Long, Map<Integer, UserDecoration>> needCheckUserDecorationMap;        //拥有限时饰品的玩家map

    private static Map<Integer, LinkedList<MammonRecord>> mammonRecordMap;

    private static Map<Integer, DecorationProto.MammonRecordListMsg.Builder> mammonRecordMsgMap;

    private static Map<Long, UserMammonOfflineAdd> mammonAddOffLineMap;

    public static Map<Integer, Date> mammonStoreFirstOpenTimeMap;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        Map<Integer, Date> tempMammonStoreFirstOpenTimeMap = new ConcurrentHashMap<>();
//      台服：4.0版本，2021年11月5日更新
        Date twDate = DateHelper.getDate(2021, 11, 5, 0, 0, 0);
        tempMammonStoreFirstOpenTimeMap.put(eChannelType.TaiWan.getValue(),twDate);

//      韩服21年12月3日
        Date krDate = DateHelper.getDate(2021, 12, 3, 0, 0, 0);
        tempMammonStoreFirstOpenTimeMap.put(eChannelType.Korea.getValue(),krDate);

//      日服21年12月9日
        Date jpDate = DateHelper.getDate(2021, 12, 9, 0, 0, 0);
        tempMammonStoreFirstOpenTimeMap.put(eChannelType.Japan.getValue(),jpDate);

//      越南4.1更的，22年1月25日
        Date vnDate = DateHelper.getDate(2022, 1, 25, 0, 0, 0);
        tempMammonStoreFirstOpenTimeMap.put(eChannelType.VN.getValue(),vnDate);

//      国服201/221/241:2021年10月24日
        Date cnDate = DateHelper.getDate(2021, 10, 24, 0, 0, 0);
        tempMammonStoreFirstOpenTimeMap.put(eChannelType.MainLand.getValue(),cnDate);
        tempMammonStoreFirstOpenTimeMap.put(eChannelType.Hun1.getValue(),cnDate);
        tempMammonStoreFirstOpenTimeMap.put(eChannelType.Hun3.getValue(),cnDate);

        //国际也是4.0更的，21年12月30日
        Date globalDate = DateHelper.getDate(2021, 12, 30, 0, 0, 0);
        tempMammonStoreFirstOpenTimeMap.put(eChannelType.Thailand.getValue(),globalDate);
        tempMammonStoreFirstOpenTimeMap.put(eChannelType.US.getValue(),globalDate);
        tempMammonStoreFirstOpenTimeMap.put(eChannelType.UK.getValue(),globalDate);

        //国服321:2022年7月2日
        Date cnDate2 = DateHelper.getDate(2022, 7, 2, 0, 0, 0);
        tempMammonStoreFirstOpenTimeMap.put(eChannelType.Mobile_DY.getValue(),cnDate2);

        //国服311:2021年12月10日
        Date cnDate3 = DateHelper.getDate(2021, 12, 10, 0, 0, 0);
        tempMammonStoreFirstOpenTimeMap.put(eChannelType.Mobile_QQ.getValue(),cnDate3);

        //国服301:2021年12月10日
        Date cnDate4 = DateHelper.getDate(2021, 12, 10, 0, 0, 0);
        tempMammonStoreFirstOpenTimeMap.put(eChannelType.XiaoChengXu.getValue(),cnDate4);

        //内网测试服
        Date innerDate = DateHelper.getDate(2022, 1, 25, 0, 0, 0);
        tempMammonStoreFirstOpenTimeMap.put(eChannelType.InnerChannel.getValue(),innerDate);

        //台服体验服也看看
        Date twTestDate = DateHelper.getDate(2021, 12, 9, 0, 0, 0);
        tempMammonStoreFirstOpenTimeMap.put(eChannelType.TaiWanTest.getValue(),twTestDate);

        mammonStoreFirstOpenTimeMap = tempMammonStoreFirstOpenTimeMap;

        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean save() {
        for(List<MammonRecord> mammonRecordList : mammonRecordMap.values()){
            List<MammonRecord> tempList = new ArrayList<>(mammonRecordList);
            for(MammonRecord mammonRecord : tempList){
                if(mammonRecord.isInsertOption()){
                    UserMammonBussiness.addMammonRecord(mammonRecord);
                }else if(mammonRecord.isUpdateOption()){
                    UserMammonBussiness.updateMammonRecord(mammonRecord);
                }
            }
        }
        for (Map.Entry<Long, UserMammonOfflineAdd> entry : mammonAddOffLineMap.entrySet()) {
            UserMammonOfflineAdd data = entry.getValue();
            if(data.isInsertOption()){
                UserMammonBussiness.addUserMammonOfflineAdd(data);
            }else if(data.isUpdateOption()){
                UserMammonBussiness.updateUserMammonOfflineAdd(data);
            }
        }
        return true;
    }

    @Override
    public boolean reloadData() {
        needCheckUserDecorationMap = UserDecorationBussiness.getNeedCheckUserDecorationMap();
        mammonRecordMap = UserMammonBussiness.getMammonRecordMap(GameConfig.MAMMON_RECORD_MAX_COUNT);
        mammonRecordMsgMap = initMammonRecordMsg();
        mammonAddOffLineMap = UserMammonBussiness.getUserMammonOfflineAdd();
        return true;
    }

    /**
     * 根据渠道id获取时间
     */
    public static Date getTimeByChannelId(int channelId){
        if (mammonStoreFirstOpenTimeMap.containsKey(channelId)) {
            return mammonStoreFirstOpenTimeMap.get(channelId);
        } else {
            //没拿到就取最晚
            return mammonStoreFirstOpenTimeMap.get(eChannelType.Mobile_DY.getValue());
        }
    }

    /**
     * 测试才会调用 设置渠道时间
     */
    public static void setChannelTime(int channelId, Date date){
        mammonStoreFirstOpenTimeMap.put(channelId, date);
    }


    public static void syncAllCrossMammon(){
        ServerGroup serverGroup = ServerGroupMgr.getServerGroupByServerId(GameServer.getInstance().getServerId());
        if(null != serverGroup && serverGroup.getCrossMammonState() > 0) {//开启跨服财神庙
            List<UserDecoration> dataList = new ArrayList<>();
            for (Map<Integer, UserDecoration> tempMap : needCheckUserDecorationMap.values()) {
                dataList.addAll(tempMap.values());
            }
            syncCrossMammonDecoration(dataList);
        }
    }

    public static void syncAllCrossMammonRecord(){
        ServerGroup serverGroup = ServerGroupMgr.getServerGroupByServerId(GameServer.getInstance().getServerId());
        ServerGroup2 serverGroup2 = ServerGroup2Mgr.getServerGroup2(eServerGroup2Type.CrossAreaMammon, GameServer.getInstance().getServerId());
        boolean crossOpen = null != serverGroup && serverGroup.getCrossMammonState() > 0;
        boolean crossAreaOpen = serverGroup2 != null;

        for(Map.Entry<Integer, LinkedList<MammonRecord>> dataEntry : mammonRecordMap.entrySet()){

            // 判断能否同步
            boolean canSync = crossOpen && MammonConfigMgr.isCrossMammonDecorationId(dataEntry.getKey())
                    || crossAreaOpen && MammonConfigMgr.isCrossAreaMammonDecorationId(dataEntry.getKey());
            if (!canSync) {
                continue;
            }

            // 可以同步
            List<MammonRecord> dataList = new ArrayList<>();
            for (MammonRecord mammonRecord : dataEntry.getValue()) {
                if (!mammonRecord.isSendCross()) {
                    dataList.add(mammonRecord);
                }
            }
            if (dataList.size() > 0) {
                syncCrossMammonRecord(dataEntry.getKey(), dataList);
            }
        }
    }

    private Map<Integer, DecorationProto.MammonRecordListMsg.Builder> initMammonRecordMsg(){
        Map<Integer, DecorationProto.MammonRecordListMsg.Builder> dataMsgMap = new ConcurrentHashMap<>();
        for(Map.Entry<Integer, LinkedList<MammonRecord>> dataEntry : mammonRecordMap.entrySet()){
            dataMsgMap.put(dataEntry.getKey(), DecorationPb.parseMammonRecordListMsg(dataEntry.getValue()));
        }
        return dataMsgMap;
    }

    public static void setUserDecoration(long userId, Map<Integer, UserDecoration> userDecorationMap){
        if(null == userDecorationMap){
            return;
        }
        //有限时饰品才加入到内存
        for(UserDecoration userDecoration : userDecorationMap.values()){
            if(userDecoration.getEndTime() > 0){
                needCheckUserDecorationMap.put(userId, userDecorationMap);
                break;
            }
        }
    }

    public static Map<Integer, UserDecoration> getUserDecorationMap(long userId){
        return needCheckUserDecorationMap.get(userId);
    }

    public static UserDecoration getUserDecoration(long userId, int decorationId){
        Map<Integer, UserDecoration> userDecorationMap = getUserDecorationMap(userId);
        if(null != userDecorationMap){
            return userDecorationMap.get(decorationId);
        }
        return null;
    }

    public static void addUserDecoration(UserDecoration userDecoration){
        if(userDecoration.getEndTime() > 0 && userDecoration.getEndTime() > System.currentTimeMillis() / 1000){
            synchronized (needCheckUserDecorationMap){
                if(!needCheckUserDecorationMap.containsKey(userDecoration.getUserId())){
                    needCheckUserDecorationMap.put(userDecoration.getUserId(), new ConcurrentHashMap<>());
                }
                needCheckUserDecorationMap.get(userDecoration.getUserId()).put(userDecoration.getDecorationId(), userDecoration);
            }
            //记入财神簿
            if(MammonConfigMgr.isMammonDecorationId(userDecoration.getDecorationId())){
                addMammonRecord(userDecoration);
            }
        }
    }

    private static void addMammonRecord(UserDecoration userDecoration){
        MammonRecord mammonRecord = new MammonRecord();
        mammonRecord.setType(userDecoration.getDecorationId());
        mammonRecord.setRecordUid(UUIDHelper.randomUUID(GameServer.getInstance().getServerId() + ""));
        mammonRecord.setUserId(userDecoration.getUserId());
        UserInfo userInfo = UserMgr.getUserInfo(userDecoration.getUserId());
        mammonRecord.setUserBaseInfo(UserMgr.parseUserBaseInfo(userInfo, GameServer.getInstance().getServerId()));
        long nowTime = System.currentTimeMillis() / 1000;
        mammonRecord.setRecordTime(nowTime);
        mammonRecord.setInsertOption();
        synchronized (mammonRecordMap){
            if(!mammonRecordMap.containsKey(mammonRecord.getType())){
                mammonRecordMap.put(mammonRecord.getType(), new LinkedList<>());
            }
        }
        LinkedList<MammonRecord> mammonRecordList = mammonRecordMap.get(mammonRecord.getType());
        synchronized (mammonRecordList){
            int index = 0;
            if(mammonRecordList.size() > 0){
                index = mammonRecordList.getLast().getMammonIndex();
            }
            index ++;
            mammonRecord.setMammonIndex(index);
            if(mammonRecordList.size() >= GameConfig.MAMMON_RECORD_MAX_COUNT){
                mammonRecordList.removeFirst();
            }
            mammonRecordList.add(mammonRecord);
            mammonRecordMsgMap.put(mammonRecord.getType(), DecorationPb.parseMammonRecordListMsg(mammonRecordList));
        }

        notifyPlayerMammonCongratulate(mammonRecord.getType(), mammonRecord.getRecordTime());
        //DataAnalyticsMgr.trackMammonWorshipPromotionGod(userInfo,mammonRecord.getType());
    }

    public static void notifyPlayerMammonCongratulate(int type, long recordTime){
        WorldMessageProto.RedHotNotifyMsg.Builder redHotMsg = WorldMessagePb.parseRedHotNotifyMsg(eRedHotNotifyType.MammonCanCongratulate.getValue(), 1, type + "");
        List<GamePlayer> playerList = GamePlayerMgr.getCopyPlayerList();
        for(GamePlayer player : playerList) {
            if(player.getModule(MammonModule.class).needPushRedHot(type, recordTime)) {//没有恭贺过的才推红点
                player.sendPacket(Protocol.U_RED_HOT_NOTIFY, redHotMsg);
            }
        }
    }

    public static void checkUserDecorationOverTime(){
        List<Long> needRemoveList = new ArrayList<>();
        for(Map.Entry<Long, Map<Integer, UserDecoration>> dataEntry : needCheckUserDecorationMap.entrySet()){
            long nowTime = System.currentTimeMillis() / 1000;
            UserMgr.checkPlayerDecorationOverTime(UserMgr.getUserInfo(dataEntry.getKey()), true);
            boolean isAllOverTime = true;
            for(UserDecoration userDecoration : dataEntry.getValue().values()){
                if(userDecoration.getEndTime() > nowTime){
                    isAllOverTime = false;
                }
            }
            if(isAllOverTime){
                needRemoveList.add(dataEntry.getKey());
            }
        }
        for(long userId : needRemoveList){
            needCheckUserDecorationMap.remove(userId);
        }
    }

    public static List<UserDecoration> getUserDecorationList(int decorationId){
        List<UserDecoration> dataList = new ArrayList<>();
        long nowTime = System.currentTimeMillis() / 1000;
        for(Map<Integer, UserDecoration> decorationMap : needCheckUserDecorationMap.values()){
            if(decorationMap.containsKey(decorationId)){
                UserDecoration userDecoration = decorationMap.get(decorationId);
                if(userDecoration.getEndTime() > nowTime) {
                    dataList.add(decorationMap.get(decorationId));
                }
            }
        }
        dataList.sort((o1, o2) -> (int) (o1.getEndTime() - o2.getEndTime()));
        return dataList;
    }

    public static DecorationProto.MammonRecordListMsg.Builder getMammonRecordListMsg(int type){
        return mammonRecordMsgMap.get(type);
    }

    public static List<MammonRecord> getMammonRecordList(int type){
        List<MammonRecord> dataList = mammonRecordMap.get(type);
        if(null != dataList){
            return new ArrayList<>(dataList);
        }
        return new ArrayList<>();
    }

    public static String getMammonCongratulateRedHotParam(GamePlayer player){
        String param = "";
        ServerGroup serverGroup = ServerGroupMgr.getServerGroupByServerId(GameServer.getInstance().getServerId());
        for(int mammonType : MammonConfigMgr.getMammonDecorationIdList()){
            boolean isFind = false;
            if (player.getModule(MammonModule.class).isInCongratulateCD(mammonType)) {
                continue;
            }
            if (MammonConfigMgr.isCrossMammonDecorationId(mammonType) || MammonConfigMgr.isCrossAreaMammonDecorationId(mammonType)) {
                if(null != serverGroup && serverGroup.getCrossMammonState() > 0) {
                    DecorationProto.MammonRecordListMsg.Builder recordListMsg = CrossDataMgr.getCrossGroupMammonRecordMsg(mammonType);
                    if (recordListMsg.getDataListCount() > 0) {
                        DecorationProto.MammonRecordTempMsg tempMsg = recordListMsg.getDataList(recordListMsg.getDataListCount() - 1);
                        if (player.getModule(MammonModule.class).needPushRedHot(mammonType, tempMsg.getRecordTime())) {
                            isFind = true;
                        }
                    }
                }
            }else {
                List<MammonRecord> dataList = getMammonRecordList(mammonType);
                if (dataList.size() > 0) {
                    MammonRecord mammonRecord = dataList.get(dataList.size() - 1);
                    if (player.getModule(MammonModule.class).needPushRedHot(mammonType, mammonRecord.getRecordTime())) {
                        isFind = true;
                    }
                }
            }
            if(isFind){
                param = param + mammonType + ",";
            }
        }
        if(!StringUtils.isNullOrEmpty(param)) {
            param = param.substring(0, param.length() - 1);
        }
        return param;
    }

    public static void syncCrossMammonDecoration(UserDecoration userDecoration){
        if(null != userDecoration){
            List<UserDecoration> dataList = new ArrayList<>();
            dataList.add(userDecoration);
            syncCrossMammonDecoration(dataList);
        }
    }

    public static void syncCrossMammonDecoration(List<UserDecoration> userDecorationList){
        ServerGroup serverGroup = ServerGroupMgr.getServerGroupByServerId(GameServer.getInstance().getServerId());
        ServerGroup2 serverGroup2 = ServerGroup2Mgr.getServerGroup2(eServerGroup2Type.CrossAreaMammon, GameServer.getInstance().getServerId());
        boolean crossOpen = null != serverGroup && serverGroup.getCrossMammonState() > 0; //开启跨服财神庙
        boolean crossAreaOpen = serverGroup2 != null; // 开启跨服的跨地区财神庙

        // 找可以上传的
        Map<Long, List<UserDecoration>> dataMap = new ConcurrentHashMap<>();
        long nowTime = System.currentTimeMillis() / 1000;
        for (UserDecoration userDecoration : userDecorationList) {
            if (userDecoration.getEndTime() <= nowTime) {
                continue;
            }
            if (crossOpen && MammonConfigMgr.isCrossMammonDecorationId(userDecoration.getDecorationId())
                    || crossAreaOpen && MammonConfigMgr.isCrossAreaMammonDecorationId(userDecoration.getDecorationId())) {
                // 可以上传
                GoodsInfo goodsInfo = GoodsMgr.getGoodsById(userDecoration.getDecorationId());
                if (null != goodsInfo && goodsInfo.getParamList().get(3).intValue() > 1) {
                    List<UserDecoration> dataList = dataMap.get(userDecoration.getUserId());
                    if (null == dataList) {
                        dataList = new ArrayList<>();
                        dataMap.put(userDecoration.getUserId(), dataList);
                    }
                    dataList.add(userDecoration);
                }
            }
        }

        // 上传
        for (Map.Entry<Long, List<UserDecoration>> dataEntry : dataMap.entrySet()) {
            if (dataEntry.getValue().size() > 0) {
                CrossGroupProto.CrossGroupMammonChangeReqMsg.Builder msg = CrossGroupProto.CrossGroupMammonChangeReqMsg.newBuilder();
                msg.setGroupId(-1); // 跨服没读这个值
                msg.setUserId(dataEntry.getKey());
                msg.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(dataEntry.getKey(), GameServer.getInstance().getServerId())));
                for (UserDecoration userDecoration : dataEntry.getValue()) {
                    msg.addDataList(DecorationPb.parseDecorationTempMsg(userDecoration));
                }
                YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_GROUP_MAMMON_CHANGE, msg);
                GamePlayerMgr.sendPacket(0, pbMsg);
            }
        }
    }

    private static void syncCrossMammonRecord(MammonRecord mammonRecord){
        if(null != mammonRecord){
            List<MammonRecord> dataList = new ArrayList<>();
            dataList.add(mammonRecord);
            syncCrossMammonRecord(mammonRecord.getType(), dataList);
        }
    }

    private static void syncCrossMammonRecord(int decorationId, List<MammonRecord> mammonRecordList){
        ServerGroup serverGroup = ServerGroupMgr.getServerGroupByServerId(GameServer.getInstance().getServerId());
        ServerGroup2 serverGroup2 = ServerGroup2Mgr.getServerGroup2(eServerGroup2Type.CrossAreaMammon, GameServer.getInstance().getServerId());
        boolean crossOpen = null != serverGroup && serverGroup.getCrossMammonState() > 0;
        boolean crossAreaOpen = serverGroup2 != null;

        boolean canSync = crossOpen && MammonConfigMgr.isCrossMammonDecorationId(decorationId)
                || crossAreaOpen && MammonConfigMgr.isCrossAreaMammonDecorationId(decorationId);
        if (!canSync) {
            return;
        }

        CrossGroupProto.CrossGroupMammonRecordChangeReqMsg.Builder msg = CrossGroupProto.CrossGroupMammonRecordChangeReqMsg.newBuilder();
        msg.setGroupId(-1); // 跨服不读这个
        msg.setDecorationId(decorationId);
        for (MammonRecord mammonRecord : mammonRecordList) {
            DecorationProto.MammonRecordTempMsg.Builder recordMsg = DecorationPb.parseMammonRecordTempMsg(mammonRecord);
            if (null != recordMsg) {
                msg.addDataList(recordMsg);
            }
        }
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_GROUP_MAMMON_RECORD_CHANGE, msg);
        GamePlayerMgr.sendPacket(0, pbMsg);
    }

    public static void crossMammonRecordChangeResp(int decorationId, List<String> recordUidList){
        List<MammonRecord> dataList = getMammonRecordList(decorationId);
        for(String recordUid : recordUidList){
            for(MammonRecord mammonRecord : dataList){
                if(Objects.equals(recordUid, mammonRecord.getRecordUid())){
                    mammonRecord.setSendCross(true);
                    dataList.remove(mammonRecord);
                    break;
                }
            }
        }
        syncAllCrossMammon();
    }

    /**
     * 获取财神称号类型
     */
    public static int getMammonType(int goodsId) {
        if (GoodsMgr.isDecorationGoods(goodsId)) {
            GoodsInfo info = GoodsMgr.getGoodsById(goodsId);
            if (info != null && info.getParamList().size() >= 4) {
                return info.getParamList().get(3).intValue();
            }
        }
        return 0;
    }

    /**
     * 获取财神称号类型
     */
    public static int getMammonType(GoodsInfo info) {
        if (info == null){
            return 0;
        }
        if (GoodsMgr.isDecorationGoods(info.getGoodsId())) {
            if (info.getParamList().size() >= 4) {
                return info.getParamList().get(3).intValue();
            }
        }
        return 0;
    }


    /**
     * 记录获得的财神成就
     * @param userId 玩家
     * @param mammonType 财神类型
     * @param value 数量
     */
    public static void addMammonCountCondition(long userId, int mammonType, int value) {
        UserMammonOfflineAdd userMammonOfflineAdd = mammonAddOffLineMap.get(userId);
        if(userMammonOfflineAdd == null){
            synchronized (mammonAddOffLineMap){
                userMammonOfflineAdd = mammonAddOffLineMap.get(userId);
                if(userMammonOfflineAdd == null){
                    userMammonOfflineAdd = new UserMammonOfflineAdd();
                    userMammonOfflineAdd.setUserId(userId);
                    userMammonOfflineAdd.setInsertOption();
                }
            }
        }
        synchronized (userMammonOfflineAdd){
            userMammonOfflineAdd.addCount(mammonType, value);
            if(userMammonOfflineAdd.isInsertOption()){
                mammonAddOffLineMap.put(userId, userMammonOfflineAdd);
            }
        }
    }

    public static Map<Long, UserMammonOfflineAdd> getMammonAddOffLineMap() {
        return mammonAddOffLineMap;
    }

    public static UserMammonOfflineAdd getUserMammonOfflineAdd(long userId){
        return mammonAddOffLineMap.get(userId);
    }

    /**
     * 处理财神称号道具
     *
     * @param activityId
     * @param userId
     * @param property
     * @param master
     * @param son
     * @return
     */
    public static void handlerMammonReward(int activityId, long userId, Property property, eLogMoneyType master, eLogMoneyType son) {
        try {
            if (property == null || property.isNothing()) {
                return;
            }
            GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
            if (player == null) {
                // 玩家不在线,加载player出来处理
                player = GamePlayerMgr.getPlayer(userId);
            }
            for (Map.Entry<Integer, BigInteger> entry : property.getGoods().entrySet()) {
                Integer decorationId = entry.getKey();
                BigInteger count = entry.getValue();
                if (player != null) {
                    player.getModule(DecorationModule.class).addDecoration(decorationId, count.longValue(), master, son);
                    ActivityInfo activityInfo = ActivityInfoMgr.getActivityInfo(activityId);
                    if (activityInfo != null) {
                        player.getModule(DecorationModule.class).setServerIds(decorationId, activityInfo.getActivityId(), activityInfo.getServerListStr());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
